//
//  CloseDialogHandler.swift
//  
//
//  Created by tank on 2021/12/4.
//

import Foundation
import WebKit
import SwiftMessages

class CloseDialogHandler: WebViewHandlerProtocol {

    static let id: String = "closeDialog"

    func handler(_ handler: WebViewHandlerProtocol, webView: WKWebViewEx?, frameInfo: WKFrameInfo, params: Any?) {

        guard let webView = webView else {
            return
        }

        guard let paramsDic = params as? [String: Any],
              let uuid = paramsDic["uuid"] as? String else {
            return
        }

        guard let dialogId = paramsDic["dialogId"] as? String else {
            runCallback(webView: webView,
                        callback: "fail",
                        uuid: uuid,
                        code: 0,
                        message: "closeDialog:fail, no dialogId",
                        paramsDic: paramsDic)
            return
        }

        guard let buttonId = paramsDic["buttonId"] as? String else {
            runCallback(webView: webView,
                        callback: "fail",
                        uuid: uuid,
                        code: 0,
                        message: "closeDialog:fail, no buttonId",
                        paramsDic: paramsDic)
            return
        }

        Dialog.shared.closeDialog(dialogId: dialogId, buttonId: buttonId)

        runCallback(webView: webView,
                    callback: "success",
                    uuid: uuid,
                    code: 0,
                    message: "closeDialog:ok",
                    paramsDic: paramsDic)
    }
}
